package com.crane.oss.common.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.crane.common.core.exception.AppException;
import com.crane.common.core.exception.BasicErrorCode;
import com.crane.common.core.exception.SysException;
import com.crane.common.framework.utils.BeanUtil;
import com.crane.oss.common.bean.request.AddAppVersionReq;
import com.crane.oss.common.bean.request.QueryAppVersionReq;
import com.crane.oss.common.bean.request.UpdateAppVersionReq;
import com.crane.oss.common.bean.resp.AppVersionResp;
import com.crane.oss.common.dao.AppVersionDao;
import com.crane.oss.common.model.AppVersion;
import com.crane.oss.common.service.AppVersionService;

import cn.hutool.core.collection.CollectionUtil;
import lombok.extern.slf4j.Slf4j;

/**
 *
 * @author tyh
 * @email 263998959@qq.com
 * @date 2020年11月10日 00时56分13秒
 */
@Service
@Slf4j
public class AppVersionServiceImpl implements AppVersionService {

    @Resource
    protected AppVersionDao appVersionDao;

    @Override
    public AppVersionResp queryAppVersion (Long id) {
        log.info("AppVersionBOImpl queryAppVersion id={}", id);
        AppVersion appVersion=appVersionDao.selectById(id);
        if (null == appVersion) {
            throw new AppException(BasicErrorCode.ENTITY_NOT_EXISTS, "appVersion");
        }
        AppVersionResp appVersionResp=new AppVersionResp();
        BeanUtil.copyProperties(appVersion, appVersionResp);
        return appVersionResp;
    }

	@Override
	public AppVersionResp queryAppVersion(QueryAppVersionReq request) {
		IPage<AppVersionResp> queryList = this.queryList(request);

		AppVersionResp resp = new AppVersionResp();
		if (null != queryList && CollectionUtil.isNotEmpty(queryList.getRecords()) && queryList.getRecords().size() > 0) {
			resp = queryList.getRecords().get(0);
		}
		return resp;
	}

    @Override
    public IPage<AppVersionResp> queryList(QueryAppVersionReq queryRequest) {
        log.info("AppVersionBOImpl queryList queryRequest={}", queryRequest);
        IPage<AppVersionResp> result = new Page<>();
        result.setCurrent(queryRequest.getPageNo());
        result.setPages(queryRequest.getPageSize());

        LambdaQueryWrapper<AppVersion> queryAppVersionWrapper = new LambdaQueryWrapper<>();
        Page<AppVersion> page = new Page<>(queryRequest.getPageNo(), queryRequest.getPageSize());
        queryAppVersionWrapper.orderByDesc(AppVersion::getCreateTime);
        IPage<AppVersion> iPage = appVersionDao.selectPage(page, queryAppVersionWrapper);
        if (CollectionUtils.isEmpty(iPage.getRecords())) {
            result.setTotal(0);
            return result;
        }
        result.setTotal(iPage.getTotal());
        List<AppVersionResp> resultList = BeanUtil.copyList(iPage.getRecords(), AppVersionResp.class);
        result.setRecords(resultList);
        return result;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int save(AddAppVersionReq addRequest) {
        log.info("AppVersionBOImpl save request={}", addRequest);
        AppVersion appVersion =new AppVersion();
        BeanUtil.copyProperties(addRequest, appVersion);
        int i = 0;
        try {
            i =appVersionDao.insert(appVersion);
        } catch (Exception e) {
            log.info("AppVersionBOImpl add e={}", e);
            throw new SysException();
        }
        return i;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int update(UpdateAppVersionReq updateRequest) {
        log.info("AppVersionServiceImpl update request={}", updateRequest);
        AppVersion appVersion=new  AppVersion();
        BeanUtil.copyProperties(updateRequest, appVersion);
        int i = 0;
        try {
            i =appVersionDao.updateById( appVersion);
        } catch (Exception e) {
            log.info("AppVersionServiceImpl add e={}", e);
            throw new SysException();
        }
        return i;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int delete(Long id) {
        log.info("AppVersionServiceImpl delete id={}", id);
        return appVersionDao.deleteById(id);
    }

}
